#include<bits/stdc++.h>
#define int long long
#define double long double
#define re register
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
using namespace std;
#ifdef ONLINE_JUDGE
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
template<typename T> inline int read(T &num){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return num=f?-x:x;
}
inline int Read()
{
	re int x=0,f=0;re char ch=getchar();
	while(!isdigit(ch)) f|=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
template<typename T> inline void write(T num)
{
	num=num<0?putchar('-'),-num:num;
	num>9?write(num/10),0:0;
	putchar(num%10+48);
}
template<typename T> inline void writeln(T num)
{
	write(num);
	putchar('\n');
}
///////////////////////////////////////////////////////////////
int n,m,w[10001],ts;
double magic,all,maxmagic;
string s,t[10001],cpy,ans;
void dfs(int now)
{
	if(clock()*1.0/CLOCKS_PER_SEC>0.9) cout<<ans.substr(1),exit(0);
	if(now>n)
	{
		magic=1,all=0;
		rep(i,1,m)
		{
//			cout<<"finding: "<<t[i]<<'\n';
			int pos=-1;
			while(pos<=n&&(pos=cpy.find(t[i],pos+1))!=-1) ++all,magic*=w[i];
		}
		if(all==0) magic=1;
		else magic=powl(magic,(double)1.0/all);
//		cout<<cpy<<" "<<magic<<'\n';
		if(magic>maxmagic) maxmagic=magic,ans=cpy;
		return;
	}
	if(cpy[now+1]!='.') dfs(now+1);
	else rep(i,'0','9') cpy[now+1]=i,dfs(now+1),cpy[now+1]='.';
}
signed main()
{
	freopen("magic.in","r",stdin);
	freopen("magic.out","w",stdout);
	read(n);read(m);
	cin>>s;s=' '+s;
	rep(i,1,m) cin>>t[i],read(w[i]),ts+=t[i].size();
//	if(n<=6&&ts<=20||n<=501&&ts<=5)
	{
		cpy=s;
		dfs(0);
		cout<<ans.substr(1);
	}
	return 0;
}

